package com.cloudkinto.common.web;

import com.alibaba.fastjson.JSONObject;
import com.cloudkinto.common.error.BaseErrorInfoInterface;
import com.cloudkinto.common.error.ErrorEnum;
import com.cloudkinto.common.i18n.ChineseMessageFactory;
import com.cloudkinto.common.i18n.EnglishMessageFactory;
import com.cloudkinto.common.i18n.JapaneseMessageFactory;
import com.cloudkinto.common.i18n.Message;
import com.cloudkinto.common.utils.StringsUtil;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.Map;

/**
 * @author zhangyy
 * @date 2020 5-9
 */
public class SingleResult<T> implements Serializable {

    /**
     * 响应代码
     */
    private String code;

    /**
     * 响应消息
     */
    private String message;

    /**
     * 响应结果
     */
    private T data;

    /**
     * 附加信息
     */
    private Map mapInfo;

    public SingleResult() {
    }

    public SingleResult(BaseErrorInfoInterface errorInfo) {
        this.code = errorInfo.getResultCode();
        this.message = errorInfo.getResultMsg();
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T result) {
        this.data = result;
    }

    public Map getMapInfo() {
        return mapInfo;
    }

    public void setMapInfo(Map mapInfo) {
        this.mapInfo = mapInfo;
    }


    /**
     * 成功
     *
     * @return
     */
    public static SingleResult success() {
        return success(null);
    }


    /**
     * 成功
     *
     * @param data
     * @return
     */
    public static <T> SingleResult<T> success(T data) {
        SingleResult result = new SingleResult();

        result.setCode(ErrorEnum.SUCCESS.getResultCode());
        result.setMessage(StringsUtil.createI18Message(ErrorEnum.SUCCESS.getResultMsg(), null));
        result.setData(data);
        return result;
    }

    /**
     * 成功
     *
     * @param messageKey
     * @param data
     * @return
     */
    public static <T> SingleResult<T> success(String messageKey, T data) {
        SingleResult result = new SingleResult();
        result.setCode(ErrorEnum.SUCCESS.getResultCode());
        result.setMessage(StringsUtil.createI18Message(messageKey));
        result.setData(data);
        return result;
    }

    public static <T> SingleResult<T> success(String messageKey, T data, Object... args) {
        SingleResult result = new SingleResult();

        result.setCode(ErrorEnum.SUCCESS.getResultCode());
        result.setMessage(StringsUtil.createI18Message(messageKey, args));
        result.setData(data);
        return result;
    }


    /**
     * 成功
     *
     * @param messageKey
     * @param data
     * @param map
     * @return
     */
    public static <T> SingleResult<T> success(String messageKey, T data, Map<String, Object> map) {
        SingleResult result = new SingleResult();
        result.setCode(ErrorEnum.SUCCESS.getResultCode());
        result.setMessage(StringsUtil.createI18Message(messageKey));
        result.setData(data);
        result.setMapInfo(map);
        return result;
    }

    /**
     * 失败
     */
    public static <T> SingleResult<T> failure(BaseErrorInfoInterface errorInfo) {
        SingleResult result = new SingleResult();

        result.setCode(errorInfo.getResultCode());
        result.setMessage(StringsUtil.createI18Message(errorInfo.getResultMsg()));
        result.setData(null);
        return result;
    }

    /**
     * 失败
     */
    public static <T> SingleResult<T> failure(String code, String messageKey) {
        SingleResult result = new SingleResult();
        result.setCode(code);
        result.setMessage(StringsUtil.createI18Message(messageKey));
        result.setData(null);
        return result;
    }

    /**
     * 失败
     */
    public static <T> SingleResult<T> failure(String messageKey, HttpServletResponse response) {
        SingleResult result = new SingleResult();
        result.setMessage(StringsUtil.createI18Message(messageKey));
        response.setStatus(500, messageKey);
        result.setData(null);
        return result;
    }

    /**
     * 失败
     */
    public static <T> SingleResult<T> failure(String code, String messageKey, T data) {
        SingleResult result = new SingleResult();
        result.setCode(code);
        result.setMessage(StringsUtil.createI18Message(messageKey));
        result.setData(data);
        return result;
    }
    public static <T> SingleResult<T> failure(String messageKey, T data) {
        SingleResult result = new SingleResult();
        result.setCode(ErrorEnum.FAILURE.getResultCode());
        result.setMessage(StringsUtil.createI18Message(messageKey));
        result.setData(data);
        return result;
    }

    /**
     * 失败
     */
    public static <T> SingleResult<T> failure(String code, String messageKey, T data, Object... args) {
        SingleResult result = new SingleResult();
        result.setCode(code);
        result.setMessage(StringsUtil.createI18Message(messageKey, args));
        result.setData(data);
        return result;
    }

    /**
     * 失败
     */
    public static <T> SingleResult<T> failure(String messageKey) {
        SingleResult result = new SingleResult();
        result.setCode(ErrorEnum.FAILURE.getResultCode());
        result.setMessage(StringsUtil.createI18Message(messageKey));
        result.setData(null);
        return result;
    }    /**
     * 失败
     */
    public static <T> SingleResult<T> failure(Message message) {
        SingleResult result = new SingleResult();
        result.setCode(ErrorEnum.FAILURE.getResultCode());
        result.setMessage(StringsUtil.createI18Message(message));
        result.setData(null);
        return result;
    }

    /**
     * 失败
     */
    public static <T> SingleResult<T> failure(String messageKey, Object... args) {
        SingleResult result = new SingleResult();
        result.setCode(ErrorEnum.FAILURE.getResultCode());
        result.setMessage(StringsUtil.createI18Message(messageKey, args));
        result.setData(null);
        return result;
    }

    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }
}
